package com.shirley.pers.zhufengfmvi.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shirley.pers.zhufengfmvi.R;
import com.shirley.pers.zhufengfmvi.entities.AlbumsDetailTracks;
import com.shirley.pers.zhufengfmvi.entities.Track;
import com.shirley.pers.zhufengfmvi.utils.DataFormatUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Description: 专辑详情界面ListVIew的数据适配器<br/>
 * <p/>
 * Copyright (c) , 2016, Shirley Xian <br/>
 * This program is protected by copyright laws. <br/>
 * ClassName: AlbumDetailAdapter<br/>
 * Date: 2016/4/8 21:23
 *
 * @author Shirley Xian
 * @version 1.0
 */
public class AlbumDetailAdapter extends BaseAdapter {
    private Context mContext;
    private AlbumsDetailTracks data;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);

    public AlbumDetailAdapter(Context context, AlbumsDetailTracks data) {
        mContext = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        int count = 0;
        if (data != null) {
            count = data.getList().size();
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        return data.getList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret;
        if (convertView != null) {
            ret = convertView;
        } else {
            ret = LayoutInflater.from(mContext).inflate(R.layout.item_basic_album_detail_list, parent, false);
        }
        ViewHolder viewHolder = (ViewHolder) ret.getTag();
        if (viewHolder == null) {
            viewHolder = new ViewHolder(ret);
            ret.setTag(viewHolder);
        }
        Track track = data.getList().get(position);
        viewHolder.item_basic_album_detail_list_title.setText(track.getTitle());
        viewHolder.item_basic_album_detail_list_playtime.setText(Long.toString(track.getPlaytimes()));
        viewHolder.item_basic_album_detail_list_comments.setText(Long.toString(track.getComments()));
        viewHolder.item_basic_album_detail_list_duration.setText(DataFormatUtils.Decimal2format(track.getDuration()/60));
        viewHolder.item_basic_album_detail_list_createdAt.setText(sdf.format(new Date(track.getCreatedAt())));

        return ret;
    }

    class ViewHolder {
        TextView item_basic_album_detail_list_title, item_basic_album_detail_list_playtime, item_basic_album_detail_list_comments, item_basic_album_detail_list_duration, item_basic_album_detail_list_play_progress, item_basic_album_detail_list_createdAt;
        ImageView item_basic_album_detail_list_download;
        View item_basic_album_detail_list_play_progress_tag;

        public ViewHolder(View itemView) {
            item_basic_album_detail_list_title = ((TextView) itemView.findViewById(R.id.item_basic_album_detail_list_title));
            item_basic_album_detail_list_playtime = ((TextView) itemView.findViewById(R.id.item_basic_album_detail_list_playtime));
            item_basic_album_detail_list_comments = ((TextView) itemView.findViewById(R.id.item_basic_album_detail_list_comments));
            item_basic_album_detail_list_duration = ((TextView) itemView.findViewById(R.id.item_basic_album_detail_list_duration));
            item_basic_album_detail_list_play_progress = ((TextView) itemView.findViewById(R.id.item_basic_album_detail_list_play_progress));
            item_basic_album_detail_list_createdAt = ((TextView) itemView.findViewById(R.id.item_basic_album_detail_list_createdAt));
            item_basic_album_detail_list_download = ((ImageView) itemView.findViewById(R.id.item_basic_album_detail_list_download));
            item_basic_album_detail_list_play_progress_tag = itemView.findViewWithTag(R.id.item_basic_album_detail_list_play_progress_tag);
        }
    }
}
